# @Time    : 2020/2/18 14:27
# @Author  : wumao
# @Email   : kanhebei@dingtalk.com

from . import home_bp
from .helper.func import view
from .helper.decorator import first_page_301
from ...models import Category, Article


@home_bp.route('/<path:path>/')
@home_bp.route('/<path:path>/page/<int:page>')
@first_page_301
def category_select(path, page=1):
    category = Category.query.filter_by(status=1, path=path).first_or_404()
    article = category.article.filter_by(status=1).paginate(page=page, per_page=category.number)
    if not article.items and page != 1:
        return '', 404

    newest = Article.query.filter_by(status=1, category_id=category.id).order_by(Article.id.desc())
    top = newest.filter(Article.thumbnail != '').filter_by(top=1)
    good = newest.filter_by(good=1)
    hot = newest.filter_by(hot=1)
    return view(
        'category.html',
        category=category,
        article=article.items,
        pagination=article,
        top=top,
        good=good,
        hot=hot,
        newest=newest
    )


